<?php
$this->load->model('services_model');
?>

<script type="text/javascript" src="<?php echo base_url(); ?>static/select2/select2.min.js" ></script>
<script type="text/javascript" src="<?php echo base_url(); ?>static/js/selectSearchGlobal.js"></script>
<!--
<link type="text/css" href="<?php echo base_url(); ?>ajaxupload/ajaxfileupload.css" rel="stylesheet" /> 
<script src="<?php echo base_url(); ?>ajaxupload/jquery.js" type="text/javascript"></script>
<script src="<?php echo base_url(); ?>ajaxupload/ajaxfileupload.js" type="text/javascript"></script>
-->



<script>
	$(function() {
		$("input[id^='datepicker']").datetimepicker({
			scrollInput: false,
			yearOffset:0,
			lang:'en',
			timepicker:false,
			datepicker:true,
			format:'d/m/Y',
			formatDate:'d/m/Y'
		});
	});
</script>
<script>
    function cancel() {
        $.post("<?= site_url() ?>search_global", function(data) {
            window.location = '<?php echo base_url(); ?>search_global/search_global';
        });
    }
    function addServTrafficSave() {                       
        var desc=$('#pTrDesc').val();
            var nilai3=desc.trim();    
        var arrTrdId=[""];
        var arrSerial=[""];     
        var arrQty=[""];
        $pJumlahI=$('#jumlahItem').val();
        var x=0;
        for($x=0;$x<$pJumlahI;$x++){
            arrTrdId[$x]=document.getElementById("pTrdID"+$x).value;
            arrSerial[$x]=document.getElementById("pTrdSerial"+$x).value;
            arrQty[$x]=document.getElementById("pTrdQty"+$x).value;
        }
        $.post("<?= site_url() ?>services/add_servTrafficSave", {
            pTrID    	:  $('#pTrID').val(),
        	pTrUserName 	: $('#pTrUserName').val(),
        	pTrUserType 	: $('#pTrUserType').val(),
        	pTrVendorName 	: $('#pTrVendorName').val(),
        	pTrStartDate 	: $('#datepicker1').val(),
        	pTrEndDate 	: $('#datepicker2').val(),
        	pTrStartHour 	: $('#pTrStartHour').val(),
        	pTrStartMinute : $('#pTrStartMinute').val(),
        	pTrEndHour 	: $('#pTrEndHour').val(),
        	pTrEndMinute 	: $('#pTrEndMinute').val(),
        	pTrDesc 	: nilai3,
        	pTrFloor	: $('#pTrFloor').val(),
        	pTrStatus	: $('#pTrStatus').val(),
        	//pTrAtasanNik	= $('#pAtasanNik').val(),
        	pTrSerial	: $('#pTrSerial').val(),
        	
        	//pRow  		:  $('#pRow').val(),
            pRow  		:  $('#jumlahItem').val(),
        	pTrdID		:  arrTrdId,
        	pTrdQty	:  arrQty,
        	pTrdSerial 	:  arrSerial
        },
        function(data) {
            window.location = '<?php echo base_url(); ?>services/traffic';
        }
        );
    }
</script>


<?php
$vDate   =date("d/m/Y");
$vHour   =date("H");
$vMinute =date("i");
 
	foreach($getTraffic as $a){   
	    $aTrID   	= $a['TRAFFIC_ID'];
    	$aTrFloor  	= $a['TRAFFIC_FLOOR'];
    	$aTrDesc    	= $a['TRAFFIC_DESC'];
    	$aTrStatus      = $a['TRAFFIC_STATUS'];
    	$aTrEndMinute 	= $a['TRAFFIC_EMINUTE'];
    	$aTrEndHour 	= $a['TRAFFIC_EHOUR'];
    	$aTrStartMinute	= $a['TRAFFIC_SMINUTE'];
    	$aTrStartHour	= $a['TRAFFIC_SHOUR'];
    	$aTrEndDate   	= $a['TRAFFIC_EDATE'];
    	$aTrStartDate 	= $a['TRAFFIC_SDATE'];
    	$aTrVendorName	= $a['TRAFFIC_VENDOR_NAME'];
    	$aTrUserType	= $a['TRAFFIC_USER_TYPE'];
    	$aTrUserName	= $a['TRAFFIC_USER_NAME'];
    	$aTrPdate1	= $a['PDATE1'];
    	$aTrPstatus1	= $a['PSTATUS1'];
    	$aTrPdate2	= $a['PDATE2'];
    	$aTrPstatus2	= $a['PSTATUS2'];
            $image          = $a['IMAGE'];
    	
    	$vParamFloor 	= $a['TRAFFIC_FLOOR'];
    	$vParamDate	= $a['TRAFFIC_EDATE'];
    	$vParamMinute	= $a['TRAFFIC_EMINUTE'];
    	$vParamHour	= $a['TRAFFIC_EHOUR'];
 	}
    
    
    if(empty($aTrPstatus1) && empty($aTrPstatus2)) {
        $getDetail=$this->services_model->getDataSik($serial);
        foreach($getDetail as $b){
    		$aTrVendorName = $b['VENDOR_NAME'];
    		$aTrUserName	 = $b['SIKJ_NAME'];
    		$aTrUserType	 = $b['VENDOR_TYPE'];
    		//echo "$aTrVendorName************<br>";
    	}
    }
    
    if(!empty($aTrPstatus1) && !empty($aTrPstatus2)) { //JIKA STATUS KOSONG, ARTINYA SUDAH TERDAFTAR DAN JAM MASUK
    	$aTrID 		= "";
    	$aTrFloor	= "";
    	$aTrDesc	= "";
    	$aTrEndMinute	= "";
    	$aTrEndHour	= "";
    	$aTrEndDate	= "";
    	$aTrStartDate	= "";
    	$aTrStartMinute	= "";
    	$aTrStartHour	= "";
    	$aTrStartDate 	= $vDate ;
    	$aTrStartHour 	= $vHour ;
    	$aTrStartMinute = $vMinute ;
    }
    
    if(empty($aTrPstatus1)) {
    	$aTrStartDate 	= $vDate ;
    	$aTrStartHour 	= $vHour ;
    	$aTrStartMinute = $vMinute ;
    }
    
    if(!empty($aTrPstatus1) && empty($aTrPstatus2)) {
    	$aTrEndDate 	= $vDate ;
    	$aTrEndHour 	= $vHour ;
    	$aTrEndMinute 	= $vMinute ;
    }
    
    //echo $aTrPstatus1."/".$aTrPstatus2; exit;
?>

<div class="col-md-offset-0" id="top">
    
    
<form id="formarea" role="form" method="post" action="" class="form-horizontal"  enctype="multipart/form-data">
    <input type="hidden" id="pTrSerial" value="<?php echo $serial;?>"/>
    
    <div class="col-md-12">
			<div class="panel panel-default">
				<div class="panel-heading">
					<h4 class="panel-title"><b class="caret"></b><b>Add Traffic</b></h4>
				</div>
				<div class="panel-body">
					<div class="col-xs-12 col-sm-6 col-md-6">
						<div class="form-group">
							<label class="control-label col-md-3">Name</label>
							<div class="col-md-7">
                            <input type="text" value="<?php echo $aTrUserName;?>" id="pTrUserName" class="form-control input-sm" />
                            </div>
						</div>
						<div class="form-group">
							<label class="control-label col-md-3">Type</label>
							<div class="col-md-4">
                            <select id="pTrUserType" class="form-control input-sm">
                                <option value="Vendor" <?php if($aTrUserType!='Tamu'&&$aTrUserType!='Telkomsel'){?>selected=""<?php }?>>Vendor</option>
                                <option value="Tamu" <?php if($aTrUserType=='Tamu'){?>selected=""<?php } ?>>Tamu</option>
                                <option value="Telkomsel" <?php if($aTrUserType=='Telkomsel'){?>selected=""<?php } ?>>Telkomsel</option>
                            </select>
							</div> 
						</div>
                        <div class="form-group">
							<label class="control-label col-md-3">Status</label>
							<div class="col-md-4">
                            <select id="pTrStatus" class="form-control input-sm">
                                <option value="In" <?php if(!(!empty($aTrPstatus1) && empty($aTrPstatus2))){?>selected=""<?php } ?>>In</option>
                                <option value="Out" <?php if(!empty($aTrPstatus1) && empty($aTrPstatus2)){?>selected=""<?php } ?>>Out</option>
                            </select>
							</div> 
						</div>
                        <div class="form-group">
							<label class="control-label col-md-3">Floor</label>
							<div class="col-md-4">
                            <select id="pTrFloor" class="form-control input-sm">
                                <?php
                                if(empty($aTrFloor)) {
                                    $i =0;
                                    foreach($getFloor as $row){
                                        $pKey 	= $row['MST_FLOOR_DESC'];
    									$pValue = $row['MST_FLOOR_DESC'];
    									if($aTrFloor == $pKey) {
    									   ?><option value="<?php echo $pKey;?>" selected=""><?php echo $pValue;?></option><?php
                                        }
                                        else {
    									   ?><option value="<?php echo $pKey;?>"><?php echo $pValue;?></option><?php
    									}
                                    }
                                }
                                else {
								    ?><option value="<?php echo $aTrFloor;?>" selected=""><?php echo $aTrFloor;?></option><?php
								}
                                ?>
                            </select>
							</div> 
						</div>
                        <div class="form-group">
							<label class="control-label col-md-3">Vendor Name</label>
							<div class="col-md-7">
                            <input type="text" value="<?php echo $aTrVendorName;?>" id="pTrVendorName" class="form-control input-sm" />
                            </div>
						</div>
                        <div class="form-group">
							<label class="control-label col-md-3">Tanggal Masuk</label>
							<div class="col-md-4">
                            <input type="text" value="<?php echo $aTrStartDate;?>" id="datepicker1" class="form-control input-sm" <?php if(!empty($aTrPstatus1) && empty($aTrPstatus2)){?>disabled=""<?php }?>/>
                            </div>
                            <div class="col-md-4">
                            <input type="text" value="<?php echo $aTrEndDate;?>" id="datepicker2" class="form-control input-sm" <?php if(!empty($aTrPstatus1) && !empty($aTrPstatus2)){?>readonly=""<?php }?>/>
                            </div>
						</div>
                        <div class="form-group">
							<label class="control-label col-md-3">Jam Masuk</label>
							<div class="col-md-2">
                            <input type="text" value="<?php echo $aTrStartHour;?>" id="pTrStartHour" class="form-control input-sm" <?php if(!empty($aTrPstatus1) && empty($aTrPstatus2)){?>readonly=""<?php }?>/>
                            </div>
                            <label class="control-label col-md-1">:</label>
                            <div class="col-md-2">
                            <input type="text" value="<?php echo $aTrStartMinute;?>" id="pTrStartMinute" class="form-control input-sm" <?php if(!empty($aTrPstatus1) && empty($aTrPstatus2)){?>readonly=""<?php }?>/>
                            </div>
						</div>
                        <div class="form-group">
							<label class="control-label col-md-3">Jam Keluar</label>
							<div class="col-md-2">
                            <input type="text" value="<?php echo $aTrEndHour;?>" id="pTrEndHour" class="form-control input-sm" <?php if(!empty($aTrPstatus1) && !empty($aTrPstatus2)){?>readonly=""<?php }?> />
                            </div>
                            <label class="control-label col-md-1">:</label>
                            <div class="col-md-2">
                            <input type="text" value="<?php echo $aTrEndMinute;?>" id="pTrEndMinute" class="form-control input-sm" <?php if(!empty($aTrPstatus1) && !empty($aTrPstatus2)){?>readonly=""<?php }?> />
                            </div>
						</div>
                        <div class="form-group">
							<label class="control-label col-md-3">Keterangan</label>
							<div class="col-md-7">
                            <textarea id="pTrDesc" class="form-control input-sm"><?php echo $aTrDesc;?></textarea>
                            </div>
						</div>
					</div>
					<div class="col-xs-6 col-sm-6 col-md-6">
						<div class="table-responsive">
                                <table class="table table-bordered table-condensed table-striped" style="background:#f9f9f9;">
                                    <thead>
                                        <tr>
                                            <th class="tblhead" style="width:5%;text-align:center;">No</th>
                                            <th class="tblhead" style="width:20%;text-align:center;">Serial No</th>
                                            <th class="tblhead" style="width:10%;text-align:center;">Quantity</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                        $getCountDetItem=$this->services_model->getCountDetItem($aTrID);
                                        $z=0;
                                        $i=0;
										$no=1;
                                        foreach ($getCountDetItem as $a) {
                                                $aCountDetItem  = $a["COUNT_DET_ITEM"];
                                                $i=0;
												$xno=1;
                                                
                                                if(	$aCountDetItem > 0){
                                                    $getTrafDet=$this->services_model->getTrafDet($aTrID);
                                                    foreach($getTrafDet as $a){
                                                        $aTrdID       = $a["TRAFFIC_DET_ID"];
														$aTrdType     = $a["TRAFFIC_DET_TYPE"];
														$aTrdSerial   = $a["TRAFFIC_DET_SERIAL#"];
														$aTrdQty      = $a["TRAFFIC_DET_QTY"];
                                                        if(empty($aTrdQty)) {
															$aTrdQty = 1 ;
														}?>
                                                        <input type="hidden" id="pTrdID<?php echo $i; ?>" value="<?php echo $aTrdID; ?>"/>
            											<tr>
            												<td><?php echo $xno; ?></td>
            												<td><input id="pTrdSerial<?php echo $i; ?>" type="text" value="<?php echo $aTrdSerial; ?>" /></td>
            												<td><input id="pTrdQty<?php echo $i; ?>" type="text" value="<?php echo $aTrdQty; ?>" /></td>
            											</tr><?php
                                                        $xno=$xno+1;
														$i=$i+1;
                                                    }
                                                }
                                                if(!empty($aTrPstatus1) && !empty($aTrPstatus2) || empty($aTrPstatus1) && empty($aTrPstatus2)){
                                                    $aTrdQty = 1 ;?>
                                                    <input type="hidden" id="pTrdID<?php echo $i; ?>" value="<?php echo $aTrdID; ?>"/>
        											<tr>
        												<td><?php echo $xno; ?></td>
        												<td><input id="pTrdSerial<?php echo $i; ?>" type="text" value="<?php echo $serial; ?>"/></td>
        												<td><input id="pTrdQty<?php echo $i; ?>" type="text" value="<?php echo $aTrdQty; ?>" /></td>
        											</tr><?php
                                                }
                                                $z++;
                                        }
                                        ?>
                                        
                                        <input type="hidden" id="jumlahItem" value="<?php echo $z;?>"/>
                                    </tbody>
                                </table>
                        </div>
                        <div class="form-group">
							<div class="col-md-3">
                                <button type="button" class="btn btn-primary btn-block" onclick="addServTrafficSave();">Save</button>
                            </div>
						</div>
					</div>
				</div>
			</div>
            
		</div>
<input type="hidden" id="pRow" value="<?php echo $xno;?>"/>
<input type="hidden" id="pTrID" value="<?php echo $aTrID;?>"/>
</form>
</div>
